package com.platform.api;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.platform.entity.CartEntity;
import com.platform.entity.GoodsEntity;
import com.platform.entity.ProductEntity;
import com.platform.entity.UserEntity;
import com.platform.service.CartService;
import com.platform.service.GoodsService;
import com.platform.service.ProductService;
import com.platform.util.ApiBaseAction;
import com.platform.utils.R;

import io.swagger.annotations.Api;

@Api(tags = "API购物车接口")
@RestController
@RequestMapping("/api/cart")
public class ApiCartController extends ApiBaseAction{
	@Autowired
	private CartService cartService;

    @RequestMapping(value= {"/list"})
    public R list() {
        try {
        	UserEntity user = getCurrentUser();
        	Map<String, Object> params  = new HashMap<String, Object>();
        	params.put("userId", user.getId());
        	List<CartEntity> carts = cartService.queryList(params);
            
            return R.ok().put("carts", carts).put("cartNum", carts.size());
        } catch (Exception e) {
        	logger.error("ApiCartController list:", e);
        	return R.error();
        }
    }
    
    @RequestMapping("/addcart/{productId}")
    public R addCart(@PathVariable("productId") Integer productId,
    		@RequestParam Map<String, Object> params) {
        try {
        	Integer buyNum ;
        	UserEntity user = getCurrentUser();
    		try {
				buyNum = Integer.valueOf(params.get("buyNum").toString());
			} catch (Exception e) {
				buyNum = 1;
			}
    		CartEntity cart = cartService.addCartProduct(productId, buyNum, user);
            return R.ok().put("cart",cart);
        } catch (Exception e) {
        	logger.error("ApiCartController addCardShop:", e);
        	return R.error();
        }
    }
    
    @RequestMapping("/cartcheck/{checked}/{cartId}")
    public R cartCheck(@PathVariable("cartId") Integer cartId,
    		@PathVariable("checked") Integer checked,
    		ModelMap map) {
        try {
        	Map<String,Object> params = new HashMap<String,Object>();
        	params.put("checked", checked);
        	params.put("id", cartId);
        	cartService.updateCheckByCartId(params);
            return R.ok();
        } catch (Exception e) {
        	logger.error("ApiCartController cartCheck:", e);
        	return R.error();
        }
    }
    
    @RequestMapping("/cartcheckall/{checked}/{userId}")
    public R cartCheckAll(@PathVariable("userId") Integer userId,
    		@PathVariable("checked") Integer checked,
    		ModelMap map) {
        try {
        	Map<String,Object> params = new HashMap<String,Object>();
        	params.put("checked", checked);
        	params.put("userId", userId);
        	cartService.updateCheckByUserId(params);
            return R.ok();
        } catch (Exception e) {
        	logger.error("ApiCartController cartCheckAll:", e);
        	return R.error();
        }
    }    
    @RequestMapping("/cartdel/{cartId}")
    public R cartCheck(@PathVariable("cartId") Integer cartId,
    		ModelMap map) {
        try {
        	cartService.delete(cartId);
            return R.ok();
        } catch (Exception e) {
        	logger.error("ApiCartController cartCheck:", e);
        	return R.error();
        }
    }

    @RequestMapping("/cartbuynum/{cartId}/{newBuyNum}")
    public R cartBuyNum(@PathVariable("cartId") Integer cartId,
    		@PathVariable("newBuyNum") Integer newBuyNum,
    		ModelMap map) {
        try {
        	Map<String,Object> params = new HashMap<String,Object>();
        	params.put("cartId", cartId);
        	params.put("newBuyNum", newBuyNum);
        	cartService.updateBuyNum(params);
            return R.ok();
        } catch (Exception e) {
        	logger.error("ApiCartController cartBuyNum:", e);
        	return R.error();
        }
    }

    @RequestMapping("/cartnum")
    public R cartProductNum() {
        try {
        	UserEntity user = getCurrentUser();
        	Map<String,Object> params = new HashMap<String,Object>();
        	params.put("userId", user.getId());
        	int cartProductNum = cartService.queryTotal(params);
            return R.ok().put("cartProductNum", cartProductNum);
        } catch (Exception e) {
        	logger.error("ApiCartController cartProductNum:", e);
        	return R.error();
        }
    }
    
}
